<template>
  <div>
    <!-- 实现数据的双向绑定 -->
    {{ value }}
    <div class="picture">
      <Picture v-model="value" />
      <!-- 点击xx删除图片 -->
    </div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
  </div>
</template>

<script>
// 导入腾讯云第三方插件
import COS from 'cos-js-sdk-v5'
import picture from '@/components/libs/picture.vue'
// 实例化腾讯云
const cos = new COS({
  SecretId: 'AKIDdDYcPvCDxReaFdlcBTVGkxsXvnPlqeAP',
  SecretKey: '1lEQM2RYQ2NREm32kCOJyOXOyHK6crqY'

})
export default {
  components: { picture },
  data() {
    return {
      // 传入默认值图片
      value: 'http://mzy-1308948032.cos.ap-guangzhou.myqcloud.com/1640336347094',
      imageUrl: ''
    }
  },
  methods: {
    // 上传成功之后的回调函数
    handleAvatarSuccess() {},
    // 上传文件之前的限制---如图片的大小类型
    beforeAvatarUpload(file) {
      const filetype = file.type === 'image/png'
      // const filesize = file.size / 1024 / 1024 < 2
      if (!filetype) {
        this.$message.error('上传图片只能是png格式')
      }
      // if (!filesize) {
      //   this.$$message.error('上传的图片不能超过2MB')
      // }
      return filetype
    },
    // 自定义上传
    httpRequest(res) {
      cos.putObject({
        Bucket: 'mzy-1308948032', /* 必须 存储桶*/
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: res.file.uid + '', /* 必须 */
        StorageClass: 'STANDARD',
        Body: res.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        console.log(err || data)
        this.imageUrl = 'http://' + data.Location
      })
    }

  }

}
</script>

<style lang='scss' scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
